package com.penghk.earth.Service;

import com.penghk.earth.entity.App;
import com.penghk.contorller.persistence.GenericDaoSupport;
//import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class AppService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    @Cacheable(value = "ehcacheGO",key = "#id")
    public App getApp(int id){
        App app = genericDaoSupport.get(App.class,1);
        return app;
    }

    public  String getAppId(int id){
        String appId = stringRedisTemplate.opsForValue().get(id+"");
        if(StringUtils.isEmpty(appId)){
            appId = getApp(id).getAppId();
            stringRedisTemplate.opsForValue().set(id+"",appId,10, TimeUnit.SECONDS);//过期时间
        }
        return appId;
    }

    public void testAop(String str1,String str2){
        System.out.println("hello "+str1+" : "+str2);
    }
}
